<?php
require_once(dirname(dirname(__FILE__)).'/init.php');
require_once(ROOT_PATH.'service/orders.php');
 	
/* 	setcookie('uid', 10);
    setcookie('time', 1509674360);
    setcookie('token', '68a6443bf43a1da8a6377ae8aca65ed9');*/

//支付选项卡同源切换
setcookie('pay',1,time()+3600,"/");

//订单对象
$order_id  = isset($_REQUEST['order_id']) ? trim($_REQUEST['order_id'] ) : '';
$orders    = new orders($Ioc,$order_id);
$backUrl   = getenv("HTTP_REFERER");
$is_wx     = $orders->is_wechat();

//获取支付宝跨平台支付码
$alipay = $orders ->get_alipay_code();
if($alipay['code'] != 200 ){
	logs('获取支付宝支付码异常:'.json_encode($alipay));
	echo "<script> alert('".$alipay['msg']."');location.href='/myorder/all'; </script>";
	die();
}


//库存队列列表化管理
$result = $orders->order_list();
if($result['code'] != 200 ){
	logs('获取订单异常:'.json_encode($result));
	echo "<script> alert('".$result['msg']."');location.href='/myorder/all'; </script>";
	die();
}


if($is_wx){//公众号内部支付

	//生成订单号
	$mode   = 'order_info';
	if(!isset($_REQUEST['code'])){
		$order  =  $orders ->main($mode);   //待支付
	}else{
		$order  =  $orders ->main($mode,'on'); //支付中
	}

	//验证状态
	if($order['code'] != 200 ){
		logs('获取订单异常:'.json_encode($order));
		$orders->router($backUrl);
	}

	//发起支付
	$order_pay = $orders ->order_pay($order['data']);
	if($order_pay['code'] != 200 ){

		//异常日志
		logs('发起支付异常:'.json_encode($order_pay));

		if($order_pay['msg'] == "201 商户订单号重复"){
			echo "<script> alert('请您使用原支付方式支付');location.href='/myorder/all'; </script>";
			die();
		}else{
			$orders->router($backUrl);
		}
	}

	//订单json
	$data   = json_encode($order_pay['data']);


}else{//浏览器支付

	$mode   = 'order_info';
	$order  =  $orders ->main($mode,'on');   //待支付

	//验证状态
	if($order['code'] != 200 ){
		logs('获取订单异常:'.json_encode($order));
		$orders->router($backUrl);
	}

	//发起支付
	$order_pay = $orders ->order_wap_pay($order['data']);
	if($order_pay['code'] != 200 ){

		//异常日志
		logs('发起支付异常:'.json_encode($order_pay));

		if($order_pay['msg'] == "201 商户订单号重复"){
			echo "<script> alert('请您使用原支付方式支付');location.href='/myorder/all'; </script>";
			die();
		}else{
			$orders->router($backUrl);
		}
	}

	//订单json
	$data   =  $order_pay['data'];logs(var_export($data,true));

}


//获取订单

?>

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>微信支付-支付</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<script src="/src/js/lib/rem.js"></script>
<style>
			*{margin:0;padding:0;}
			/*.pay_process_con{width:100%;height:1.19rem;}*/
			.pay_process{
				height:2.99rem;
				width:5.26rem;
				position:absolute;
				top:20%;
				left:8%;
				background:#ffffff;
				padding-top:0.54rem;
				border-radius:0.1rem;
                z-index: 1;
			}
			/*支付宝中间层*/
			.wxSuccess_pay{
				height:0.9rem;
				width:6.1rem;
				font-size:0.24rem;
				background:#29adfc;
				display:block;
				text-align:center;
				margin:0 0.17rem;
				border-radius:0.05rem;
				color:#ffffff;
				line-height:0.9rem;
			}
			.wxSuccess_p_con p:nth-of-type(5){
				width:100%;
				height:0.51rem;
				line-height:0.51rem;
				text-indent: 2.18rem;
				font-size:0.26rem;
				color:#202020;
				/*font-weight:700;*/
			}
			.wxSuccess_p_con p:nth-of-type(4){
				width:100%;
				height:0.51rem;
				line-height:0.51rem;
				text-indent: 1.99rem;
				font-size:0.26rem;
				color:#202020;
			/*	font-weight:700;*/
			}
			.wxSuccess_p_con p:nth-of-type(3){
				width:100%;
				height:0.51rem;
				line-height:0.51rem;
			    text-align:center;
				font-size:0.26rem;
				color:#202020;
				/*font-weight:700;*/
			}
			.wxSuccess_p_con p:nth-of-type(2){
				width:100%;
				height:0;
				line-height:0;
				text-align:center;
				font-size:0.26rem;
				color:#137df6;
				font-weight: 700;
				margin-bottom:0.23rem;
			}
			.wxSuccess_p_con p:nth-of-type(1){
				width:100%;
				height:0.47rem;
				line-height:0.47rem;
				text-align:center;
				font-size:0.26rem;
				color:#29adfc;
				font-weight:normal;
			}
			.wxSuccess_p_con{
				width:100%;
				height:2.7rem;
				border-bottom:0.1rem solid #f5f4f2;
			}
			/*订单编号*/
			.wxSuccess_top span{
				width:1.48rem;
				height:0.78rem;
				color:#ffffff;
				font-size:0.23rem;
				position:absolute;
				text-align:center;
				line-height:0.78rem;
				right:0;
				top:0;
			}
			.wxSuccess_top h2{
				width:100%;
				height:0.78rem;
				line-height:0.78rem;
				text-align:center;
				color:#ffffff;
				font-size:0.27rem;
				font-weight: normal;
			}
			.wxSuccess_top img{
				display:block;
				height:0.37rem;
				width:0.41rem;
				position:absolute;
				top:0.25rem;
				left:0.25rem;
			}
			.wxSuccess_top{
				width:100%;
				height:0.78rem;
				background:#29adfc;
				position:relative;
				margin-bottom:0.33rem;
			}
			/*.pay_process_con img{
				display:block;
				height:1.19rem;
				width:1.64rem;
				position:absolute;
				top:0;
				right:0;
			}*/
			.pay_process_p{
				width:5.36rem;
				height:0.92rem;
				margin-bottom:0.3rem;
			}
			.pay_process_p p:nth-of-type(1){
				width:5.36rem;
				height:0.45rem;
				text-align:center;
				line-height:0.45rem;
				font-size:0.22rem;
				color:#000000;
			}
			.pay_process_p p:nth-of-type(2){
				width:5.36rem;
				height:0.47rem;
				text-align:center;
				line-height:0.47rem;
				font-size:0.22rem;
				color:#000000;
			}
			.pay_process_1{
				width:auto;
				height:1.14rem;
				margin:0 auto;
				border-top:1px solid #f0f0f0;
				border-bottom:1px solid #f0f0f0;
				padding-top:0.3rem;
			}
			.pay_process_1 time{
				color:#ff9595;
				font-weight:700;
			}
			.pay_process_p time{
				color:#ff9595;
				font-weight:700;
			}
			.pay_process_1 p{
				width:5.36rem;
				height:0.4rem;
				line-height:0.4rem;
				text-align:center;
				font-size:0.23rem;
			}
			/*微信支付宝的样式*/
			.member_wx1 img{
				display:block;
				width:0.57rem;
				height:0.38rem;
				position:absolute;
				top:0.2rem;
				left:0;
			}
			.member_wx1 span{
				display:block;
				height:0.26rem;
				width:0.26rem;
				border-radius:50%;
				border:0.01rem solid #d2d1cf;
				position:absolute;
				right:0;
				top:0.24rem;
			}
			.member_wx1{
				width:6.06rem;
				height:0.8rem;

				text-indent: 0.64rem;
				line-height:0.8rem;
				color:#3f3f3f;
				font-size:0.26rem;
				position:relative;
				margin:0 auto;
			}
			#zi_fu{
				width:0.52rem;
				height:0.41rem;
			}
			.bei_jing{
				height:100%;
				width:100%;
				background:#000000;
				opacity:0.5;
				position:absolute;
				top:0;
				left:0;
			}
			/*提示弹窗样式*/
			.model_w p:nth-of-type(2) span{
				flex:1;
				color:#0870d7;
				font-size:0.28rem;
				height:0.7rem;
				line-height:0.7rem;
				text-align:center;
			}
			.model_w p:nth-of-type(2) span:nth-of-type(1){
				border-right:0.01rem solid #cccccc;
			}
			.model_w p:nth-of-type(2){
				height:0.7rem;
				width:4.62rem;
				border-top:0.01rem solid #d9d4cc;
				display:flex;
			}
			.model_w p:nth-of-type(1){
				height:0.3rem;
				line-height:0.3rem;
				text-align:center;
				color:#000000;
				font-size:0.2rem;
				margin-bottom: 0.25rem
			}
			.model_w h3{
				width:4.62rem;
				height:0.56rem;
				text-align:center;
				line-height:0.56rem;
				font-size:0.24rem;
				color:#000000;
				font-weight:normal;
			}
			.model_w{
				height:1.82rem;
				width:4.62rem;
				background:#ffffff;
				border-radius:0.1rem;
				position:fixed;
				top: 38%;
				left: 14%;
				z-index:2;
			}
			.pay_mode.show{
				  background:url(/src/image/blue_2.jpg) no-repeat;
                  background-size: contain;
                  border:0;
			}
</style>
</head>
<body>

	<div>
		<div class="wxSuccess_top">
			<a href="/"><img src="/src/image/house_m.jpg" alt=""></a>
			<h2>已提交订单</h2>
			<span id="orderInfo">订单详情</span>
		</div>
		<div class="wxSuccess_p_con">
			<p>支付时间还剩:<time id="time_m"></time></p>
			<p></p>

			<p>订单编号:<time><?php echo $order['data']['order_sn'];?></time></p>
			<p>订单金额:<time>￥<?php echo $order['data']['order_amount'];?></time></p>
			<?php if(WxPay_Debug == true) { echo '开启微信支付调试模式'; }?>

		</div>
		<div class="member_wx1" style=''>
			微信支付
			<img src="/src/image/wx.jpg" alt="">
			<span class="pay_mode show" data-id="1"></span>
		</div>
		<p style="height:0.02rem;background:#f0f0f0"></p>
		<div class="member_wx1" style="">
			支付宝支付
			<img src="/src/image/zifu.jpg" alt="" id="zi_fu">
			<span class="pay_mode"  data-id="2"></span>
		</div>
		<p style='margin-bottom:0.6rem;height:0.1rem;background:#f5f4f2'></p>
		<span class="wxSuccess_pay"  onclick="callpay()" id="payBut" data-id="1">立即支付</span>

		<div class="pay_process" style="display:none;">
			<!--<div class="pay_process_con" style="display:none;">
				<img src="src/image/arrow_2.jpg" alt="">
			</div>-->
			<div class="pay_process_p">
				<p>请在右上角箭头指向的<time>菜单</time>菜单中选择"<time>浏览器"打开</time></p>
				<p>在浏览器中完成支付</p>
			</div>
			<div class="pay_process_1">
                     <p>是否已经完成付款?</p>
				     <p>请确保支付完成后,点击<time>完成付款</time></p>
			</div>
		</div>
	</div>
	<!--背景弹窗-->
	<div class="bei_jing" style="display:none"></div>
	<!--提示弹窗-->
	<div class="model_w" style="display:none">
		<h3>提示</h3>
		<p>你的支付是否已经完成</p>
		<p>
			<span  onclick="question()">支付遇到问题</span>
			<span  onclick="to_order()">支付已经完成</span>
		</p>
	</div>

<script src="/src/js/lib/jquery-3.0.0.min.js"></script>
<script type="text/javascript">

	//根域名
	const  HostUrl  = window.location.protocol+'//'+window.location.host;
	//订单号
	const  order_id = "<?php echo $order_id;?>";
	var    date     = "<?php echo $order['data']['add_time'];?>";

//微信内
<?php if($is_wx){ ?>

	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',
			<?php echo $data; ?>,
			function(res){
				if(res.err_msg == "get_brand_wcpay_request:ok" ) {
              		to_order();
				}
				if(res.err_msg == "get_brand_wcpay_request:cancel" ) {
					//cancelPay();
				}    
				WeixinJSBridge.log(res.err_msg);
				//alert(res.err_code+res.err_desc+res.err_msg);
			}
		);
	}

	function callpay()
	{
		//遮住
		view_But()
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}

	function cancelPay(){

	  let url  =  HostUrl+'/php/v1/orderApi.php?mode=cancelPay'; 
	  let Data =  {"order_id":order_id};
      $.ajax({
           type: "POST",
           url: url,
           async: true,
           data: Data,
           success: function(temp){
                if(temp.code != 200){
                	alert('取消支付异常');
                	console.log('取消支付异常');        
                }
               //alert('取消支付成功');   
               console.log(temp.msg);  
           }
        });
	}

<?php }else{ ?>

	function callpay()
	{
		setTimeout("view_But()",5000);
		window.location.href= "<?php echo $data['mweb_url'];?>"
	}

<?php } ?>

	//支付显示
	function view_But(){
		 $('.bei_jing').show();
         $('.model_w').show();
	}	

	//支付完成
	function to_order(){
		check_pay();
	}

	//支付疑问
	function question(){
        $('.bei_jing').hide();
        $('.model_w').hide();
	}

	//完成付款验证
	function check_pay(){
	  let url  =  HostUrl+'/php/v1/orderApi.php?mode=check_pay'; 
	  let Data =  {"order_id":order_id};
      $.ajax({
           type: "POST",
           url: url,
           async: true,
           data: Data,
           success: function(temp){
                if(temp.code == 200){
                	window.location.href= "/pay_success?order_id="+order_id;      
                }  
           }
        });	
	}

//支付倒计时时间显示计算
 function getCountDown(timestamp){
             setInterval(function(){
                 var nowTime = new Date();
                 var endTime = timestamp.getTime() + (1000*30*60);

                 var t = endTime - nowTime.getTime();
     	           var min=Math.floor(t/1000/60%60);
                    var sec=Math.floor(t/1000%60);

                 if (min < 10) {
                      min = "0" + min;
                 }
                 if (sec < 10) {
                      sec = "0" + sec;
                 }
                 var countDownTime =min + ":" + sec;
                 $("#time_m").html(countDownTime);

             },1000);
         }
//调用函数：
getCountDown(new Date(date.replace(/-/g,"/")));//调取时间倒计时的函数

	$(function(){

		//跳转详情
	    $("#orderInfo").click(function(){
	       window.location.href='/Orderdetails?order_id='+order_id;
	    })

		//切换支付方式
		$('.pay_mode').click(function(){

			//选中
			$('.pay_mode').removeClass('show');
			$(this).addClass('show');

			//附加
			let pay_id = $(this).attr('data-id');

			//注入
			$("#payBut").attr('data-id',pay_id);
			
			//微信切换
			if(pay_id == 1){
			   window.location.href= "/php/v1/wxPay.php?order_id="+order_id; 
			}

			//支付宝切换
			if(pay_id == 2){
			   window.location.href= "/alipay/aliPay.php?token="+"<?php echo $alipay['data']['code'];?>";
			}

		})

        $('.bei_jing').click(function(){
            $(".pay_process").hide();
            $(this).hide();
            $('.model_w').hide();
		})
	})

</script>
</body>
</html>
